﻿/*
 ***************************************************************************
 * Microsoft Visual C# .NET
 * 名称：       App_Sales_SaleAchievementSrh
 * 类型：       控制层
 * 功能说明：   员工业绩检索页面
 * 作者：       黄鑫
 * 创建日期：   2007/09/03
 * 修改日期     修改者      修改内容
 * 
 * 
 * ---
 ***************************************************************************
*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using log4net;
using Shpm.App.Search.Bean;
using Shpm.App.Search.Dao;
using Shpm.App.Master.Bean;
using Shpm.App.Master.Dao;
using Shpm.App.Commons.Db.Beans.Master;
using Shpm.System;
using Shpm.App.Commons.Utils;
using Shpm.System.Validator;
using Shpm.System.Web.Control;

public partial class SaleAchievementSrhForm : System.Web.UI.Page
{
    protected static ILog log = LogManager.GetLogger(typeof(SaleAchievementSrhForm));
    private const string COMMAND_SEARCH = "DoSearch";

    //Form Load
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack == false)
        {

            // 接收传递参数并初始化控件
            if (Context.Items.Contains("ParamEmployeeId"))
            {
                txtEmployeeId.Text = Convert.ToString(Context.Items["ParamEmployeeId"]);
                ViewState["ParamCommand"] = COMMAND_SEARCH;
            }
            else
            {
                SetEmployeeId();
            }
            txtBeginDate.Text = Convert.ToString(Context.Items["ParamBeginDate"]);
            txtEndDate.Text = Convert.ToString(Context.Items["ParamEndDate"]);

            // 接收传递参数为检索条件
            ViewState["ParamEmployeeId"] = Convert.ToString(Context.Items["ParamEmployeeId"]);
            ViewState["ParamIsEmptyDate"] = Convert.ToString(Context.Items["ParamIsEmptyDate"]);
            if (Convert.ToString(ViewState["ParamIsEmptyDate"]) != "1")
            {
                if (string.IsNullOrEmpty(txtBeginDate.Text))
                {
                    txtBeginDate.Text = DateUtils.convertToString(DateTime.Today);
                }
                if (string.IsNullOrEmpty(txtEndDate.Text))
                {
                    txtEndDate.Text = DateUtils.convertToString(DateTime.Today);
                }
            }
            ViewState["ParamEmployeeId"] = txtEmployeeId.Text;
            ViewState["ParamBeginDate"] = txtBeginDate.Text;
            ViewState["ParamEndDate"] = txtEndDate.Text; 


            // 接收传递参数为分页编号
            if (Context.Items.Contains("ParamPageIndex"))
            {
                viewSrhRst.PageIndex = Convert.ToInt32(Context.Items["ParamPageIndex"]);
            }
        }

    }

    // Step Click
    protected void viewSrhRst_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (Request["x"] == null || Request["y"] == null)
            return;
        string url = "~/App/Search/SaleAchievementDetailSrhForm.aspx";
        int index = Convert.ToInt32(e.CommandArgument);
        // Get the Key value from the GridView control.
        DataKey key = viewSrhRst.DataKeys[index];
        string employeeId = Convert.ToString(key.Value);
        // 传递分页编号
        Context.Items["ParamPageIndex"] = viewSrhRst.PageIndex;
        Context.Items["EmployeeId"] = employeeId;
        Context.Items["ParamBeginDate"] = ViewState["ParamBeginDate"];
        Context.Items["ParamEndDate"] = ViewState["ParamEndDate"];
        Context.Items["ParamEmployeeId"] = ViewState["ParamEmployeeId"];
        Context.Items["ParamIsEmptyDate"] = ViewState["ParamIsEmptyDate"];
        ControllerUtils.Transfer(Context, url, true);  

    }

    // page index change
    protected void viewSrhRst_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        // 设置分页编号
        viewSrhRst.PageIndex = e.NewPageIndex;
    }

    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
        if (COMMAND_SEARCH.Equals(ViewState["ParamCommand"]))
        {
            SaleAchievementSrhBean dataBean = new SaleAchievementSrhBean();
            
            dataBean.EmployeeId = ViewState["ParamEmployeeId"] == null ? null:Convert.ToString(ViewState["ParamEmployeeId"]);
            dataBean.BeginDate = ViewState["ParamBeginDate"] == null ? null : Convert.ToString(ViewState["ParamBeginDate"]);
            dataBean.EndDate = ViewState["ParamEndDate"] == null ? null : Convert.ToString(ViewState["ParamEndDate"]);
            SaleAchievementSrhDao dao = new SaleAchievementSrhDao();
            IList dataList = dao.ListAchievementSrh(dataBean);
            viewSrhRst.DataSource = dataList;
            if (dataList.Count == 0)
            {
                ((Design_MasterPage)this.Master).InfoMessage =
                    HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "E0005").ToString();
                tblResult.Visible = false;
            }
            else
            {
                tblResult.Visible = true;
            }
            viewSrhRst.DataBind();
        }
    }

    // adjust bind data
    protected void viewSrhRst_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        // do validate check
        ComValidateChecker vd = new ComValidateChecker();
        vd.CheckItems.Add(new CheckDate(txtBeginDate.Text, "yyyy/MM/dd", new ComMessage("E0037", "开始日期")));
        vd.CheckItems.Add(new CheckDate(txtEndDate.Text, "yyyy/MM/dd", new ComMessage("E0037", "结束日期")));
        if (!vd.DoValidator())
        {
            ((Design_MasterPage)this.Master).InfoMessage = vd.ErrorMessage;
            return;
        }
        vd.CheckItems.Add(new CheckDateRange(txtBeginDate.Text, txtEndDate.Text, new ComMessage("E0330", new string[] { "开始日期", "结束日期" })));
        if (!vd.DoValidator())
        {
            ((Design_MasterPage)this.Master).InfoMessage = vd.ErrorMessage;
            return;
        }

            // 保存控件状态为检索条件
            ViewState["ParamEmployeeId"] = txtEmployeeId.Text == "" ? null : Convert.ToString(txtEmployeeId.Text);
            ViewState["ParamBeginDate"] = txtBeginDate.Text == "" ? null : txtBeginDate.Text;
            ViewState["ParamEndDate"] = txtEndDate.Text == "" ? null : txtEndDate.Text;
            ViewState["ParamCommand"] = COMMAND_SEARCH;
            if (string.IsNullOrEmpty(txtBeginDate.Text) && string.IsNullOrEmpty(txtEndDate.Text))
            {
                ViewState["ParamIsEmptyDate"] = "1";
            }
            else
            {
                ViewState["ParamIsEmptyDate"] = "0";
            }
            viewSrhRst.PageIndex = 0;
    }
    //设置职员编号
    private void SetEmployeeId()
    {
        SysUserManager manager = new SysUserManager(Context);
        MstEmployeeBean empBean = manager.getSysUserBean() == null? null :manager.getSysUserBean().getEmployeeBean();
        if (empBean != null && empBean.Role != Constants.ROLE_SALE_MANAGER)
        {
            ViewState["EmployeeId"] = empBean.EmployeeId;
        }
        else
        {
            ViewState["EmployeeId"] = string.Empty;
        }
        txtEmployeeId.Text = (string)ViewState["EmployeeId"];
        ViewState["ParamCommand"] = COMMAND_SEARCH;
    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        MainMenuManager manager = new MainMenuManager(Context);
        MainMenuBean menuBean = manager.GetCurrentMenu();

        manager.Forward(menuBean);
    }
}
